package com.ruoyi.source.websocket.com;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.source.service.ISrcCropInfoService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
//@EnableScheduling
public class WebSocketScheduling {
    @Autowired
    private ISrcCropInfoService iSrcCropInfoService;
    /**
     * 3秒执行一次
     * @return
     * @throws Exception
     */
    @XxlJob("aaaa")
    public String send() throws Exception {
        Map<String,Object> map = new HashMap<>();
        List<Map<String, Object>> mapList = iSrcCropInfoService.selectCropTypeCount();
        //List<String> dictLabelList = new ArrayList<>();
        //List<Long> countList = new ArrayList<>();
        List<String> dictLabelList = mapList.stream().map(ss -> ss.get("dictLabel").toString()).collect(Collectors.toList());
        List<Long> countList = mapList.stream().map(ss -> Long.valueOf(ss.get("countCrop").toString())).collect(Collectors.toList());
        /*for (Map<String, Object> stringObjectMap : mapList) {
            System.err.println(stringObjectMap);
            String dictLabel = (String) stringObjectMap.get("dictLabel");
            dictLabelList.add(dictLabel);
            Long count  = (Long) stringObjectMap.get("countCrop");
            countList.add(count);
        }*/
        HashMap<String, Object> serverMap = new HashMap<>();
        serverMap.put("dictLabelList",dictLabelList);
        serverMap.put("countList",countList);
        map.put("server",serverMap);
        map.put("pie",mapList);
        JSONObject jsonObject =  new JSONObject(map);
        WebSocketServer.sendAllMessage(jsonObject.toString());
        return jsonObject.toString();
    }
}